package com.baosight.sczx.zm.cn.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cn.domain.ZMCNMcmodule;
import com.baosight.sczx.zm.cn.domain.ZMCNMtcl;
/**
 * 煤炭产量数据管理
 */
public class ServiceZMCN70 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "50");//设置每页默认显示记录数
		EiInfo outInfo = this.query(inInfo);
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
//		outInfo.addBlock(getMines());
//		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
//		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
//		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
//		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		return outInfo;
	}
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		if(null != inInfo.getBlock("inqu_status")){
			String fdStatus = inInfo.getBlock("inqu_status").getCellStr(0, "fdStatus");
			String fdVirtualDate = inInfo.getBlock("inqu_status").getCellStr(0, "fdVirtualDate");			
			String fdMiningId = inInfo.getBlock("inqu_status").getCellStr(0, "fdMiningId");
			if(null != fdStatus && !("").equals(fdStatus)){
				StaticTools.setQueryParameters(inInfo, "fdStatus", fdStatus, true);//添加查询条件 
			}
			if(null != fdVirtualDate && !("").equals(fdVirtualDate)){
				StaticTools.setQueryParameters(inInfo, "fdVirtualDate", fdVirtualDate, true);//添加查询条件 
			}
			if(null != fdMiningId && !("").equals(fdMiningId)){
				StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);//添加查询条件 
			}
			}		
		EiInfo outInfo = super.query(inInfo, "ZMCNMtcl.query", new ZMCNMtcl());
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ch.status", new String[] { "草拟", "上报", "已发布" }, new String[] { "草拟", "上报", "已发布" }, false));// 状态
		outInfo.addBlock(getMines());
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		outInfo.addBlock(getCoalCode());//煤样编码
		return outInfo;
	}	
	
	/**
	 * 煤样编码下拉框
	 * @return
	 */
	public EiBlock getCoalCode() {
		EiBlock block = new EiBlock("cmes.ch.coalcode");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			Map<String, String> params=new HashMap();
			params.put("fdStatus", "生效");
			_listData = dao.query("ZMCNMcmodule.query", params);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_map = new HashMap<String, String>();
			ZMCNMcmodule _temp = (ZMCNMcmodule)_listData.get(i);
			_map.put("value", _temp.getFdMyCode());
			_map.put("display",_temp.getFdMyDetail());
			_list.add(_map);
		} 
		block.setRows(_list);
		return block;
	}	
	
	public EiBlock getMines() {
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCGTsPg.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}

	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo outinfo = new EiInfo();
		ZMCNMtcl _dom = new ZMCNMtcl();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
//		String fdStatus = "草拟";
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
		String fdMiningName = (String) UserSession.getInSessionProperty("miningName");//矿名称的默认值
		String fdCurrentDate =  StaticTools.getDate();
		String fdReportTime = StaticTools.getDateTime();
		String fdReportPerson = (String) UserSession.getInSessionProperty("UserCname");
		String fdReportDepartment =  (String) UserSession.getInSessionProperty("orgName");
		String fdReportDepartmentId =  (String) UserSession.getInSessionProperty("orgCode");
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			_dom.setFdReportPerson(fdReportPerson);
			_dom.setFdCurrentDate(fdCurrentDate);
			_dom.setFdReportDepartmentId(fdReportDepartmentId);
			_dom.setFdReportDepartment(fdReportDepartment);
			_dom.setFdReportTime(fdReportTime);
			_dom.setFdMiningId(fdMiningId);
			_dom.setFdMiningName(fdMiningName);
//			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
//			_dom.setFdStatus(fdStatus);
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CN_MTCL"));			
			this.dao.insert("ZMCNMtcl.insert", _dom);							
		}
		outinfo = this.query(inInfo);
		outinfo.setStatus(1);
		outinfo.setMsg("操作" + rowcount + "记录成功");
		return outinfo;
	}
	 
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p> 
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out=new EiInfo();
		ZMCNMtcl _dom = new ZMCNMtcl();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
//			params.put("fdVirtualDate", _dom.getFdVirtualDate());

		    this.dao.update("ZMCNMtcl.update", _dom);			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
		
//		EiInfo outInfo = new EiInfo();
//		ZMCNMtcl _dom = new ZMCNMtcl();
//		EiBlock eiBlock = inInfo.getBlock("result");
//		eiBlock.setBlockMeta(_dom.eiMetadata);
//		int rowcount = eiBlock.getRowCount();
//		String fdCurrentDate =  StaticTools.getDate();
//		String fdReportTime = StaticTools.getDateTime();
//		String fdReportPerson = (String) UserSession.getInSessionProperty("UserCname");
//		String fdReportDepartment =  (String) UserSession.getInSessionProperty("orgName");
//		String fdReportDepartmentId =  (String) UserSession.getInSessionProperty("orgCode");
//		for (int i = 0; i < rowcount; i++) {
//			_dom.fromMap(eiBlock.getRow(i));
//			Map<String, String> params = new HashMap<String, String>();
//			params.put("fdVirtualDate", _dom.getFdVirtualDate());
//			params.put("fdMiningName", _dom.getFdMiningName());
//			boolean validate = this.validate(inInfo, _dom, params);
//			if(validate){
//				return inInfo;
//			}
//				_dom.setFdReportPerson(fdReportPerson);
//				_dom.setFdCurrentDate(fdCurrentDate);
//				_dom.setFdReportDepartmentId(fdReportDepartmentId);
//				_dom.setFdReportDepartment(fdReportDepartment);
//				_dom.setFdReportTime(fdReportTime);
////				_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
//			this.dao.update("ZMCNMtcl.update", _dom);
//		}
//			outInfo = this.query(inInfo);
//			outInfo.setStatus(-1);
//			outInfo.setMsg("只能修改草拟状态的记录!"); 
//			return outInfo;
////		}
////		}
////		outInfo = this.query(inInfo);
////		outInfo.setStatus(1);
////		outInfo.setMsg("操作" + rowcount + "记录成功");
////		return outInfo;
	}	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p> 
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{

		EiInfo outInfo = null;
		
		List<String> _list = new ArrayList<String>();
		Map<String,List<String>> params = new HashMap<String,List<String>>();

		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{

			EiInfo _temp = super.delete(inInfo, "ZMCNMtcl.delete");
	        outInfo = this.query(inInfo);
	        outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		
		return outInfo;
	}
	/**
	 * <p>Function: report</p>
	 * <p>Description: 上报</p> 
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCNMtcl _dom = new ZMCNMtcl();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

			this.dao.update("ZMCNMtcl.report",_dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	
	}
		
	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		ZMCNMtcl _dom = new ZMCNMtcl();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String oper=(String)inInfo.get("efCurButtonEname");
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			if(oper.equalsIgnoreCase("update")){
				params.put("fdKeyId",_dom.getFdKeyId());
			}
			String fdMiningName = _dom.getFdMiningName();
			String fdVirtualDate = _dom.getFdVirtualDate();
			
			params.put("fdMiningName", fdMiningName);
			params.put("fdVirtualDate", fdVirtualDate);
			
			List result = dao.query("ZMCNMtcl.validate", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				ZMCNMtcl _temp = (ZMCNMtcl)result.get(0);
			
				StringBuffer msg = new StringBuffer("提交失败，");
				if(_temp.getFdVirtualDate() != null && _temp.getFdVirtualDate().equals(fdVirtualDate)) {
					msg.append("");
				}
				if(_temp.getFdMiningName() != null && _temp.getFdMiningName().equals(fdMiningName)){
					msg.append("");
				}
				
	
			msg.append("每矿每天只能提交一份产量报告！");
			
			inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}		
		
	
//	private boolean validate(ZMCNMtcl _dom){
//		  String fdStatus = _dom.getFdStatus();
//		  if("草拟".equals(fdStatus)){
//			 return true; 
//		  }else
//			  return false;
//		  
//	}
} 